package com.xujiang.myblog.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.xujiang.common.ui.CMD;
import com.xujiang.common.uiutil.ButtonCountdown;
import com.xujiang.common.util.ToolBarHelper;
import com.xujiang.myblog.Base.CPRXFragment;
import com.xujiang.myblog.R;
import com.xujiang.myblog.network.okhttp.MOkhttp;
import com.xujiang.myblog.ui.TestActivity;
import com.xujiang.myblog.util.L;
import com.xujiang.myblog.util.UIUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xujiang on 2017/4/12.
 */

public class OtherFragment extends CPRXFragment {
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.up)
    Button up;
    @BindView(R.id.home)
    Button home;
    @BindView(R.id.test)
    Button test;
    @BindView(R.id.cmd) Button cmd;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_other, container, false);
        ButterKnife.bind(this, view);
        initToolbar(view);
        L.i(TAG, "OtherFragment Blog Fragment");
        ButtonCountdown.setButtonClockandEnable(getContext(), test);
        return view;
    }

    private void initToolbar(View view) {
        ToolBarHelper toolbarhelper = ToolBarHelper.with(view);
        Toolbar toolbar = toolbarhelper.getToolbar();
        try {
            setAll(toolbar, R.mipmap.ic_launcher, R.mipmap.user_portrait_hint_sm,
                    "Title test can show how long", "SubTitle test can show how long",
                    R.menu.base_toolbar_menu);
            setDefaultOnclicklistener(toolbar);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({ R.id.up, R.id.home })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.up:
                //192.168.123.1
                File file1 = new File(
                        Environment.getExternalStorageDirectory().getAbsolutePath() + "/testfile/test.jpg");
                File file2 = new File(
                        Environment.getExternalStorageDirectory().getAbsolutePath() + "/testfile/test2.jpg");
                List<File> files = new ArrayList<>();
                files.add(file1);
                files.add(file2);
                Map<String, String> maps = new HashMap<>();
                maps.put("one", "one");
                maps.put("two", "two");
                MOkhttp.upFile2("http://192.168.123.1:5000/api_test/files/phoneupfile", files, maps, "file");
                break;
            case R.id.home:
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            MOkhttp.post("http://192.168.123.1:5000/api_test/files/phoneupfile",
                                    "{'dsfs':'fsdsga','sfdaf':'fasf'}");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
        }
    }

    //fragment的显示监听
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            Log.i("hepan", "显示封面3");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick(R.id.test)
    public void onViewClicked() {
        UIUtils.startActivity(getContext(), TestActivity.class);
        ButtonCountdown.limitClock(test, getContext());
        new CountDownTimer(10 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                Log.i("-------->>>>>>", millisUntilFinished + ":" + millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                System.out.print("onFinish");
            }
        }.start();
    }

    @OnClick(R.id.cmd)
    public void onViewClickedcmd() {
        UIUtils.startActivity(getContext(), CMD.class);
    }
}
